
function formSubmit () {
 var input = document.getElementById('input');
 var password = document.getElementById('password');
 var btn = document.getElementById('btn');
 btn.onclick = function () {
  var username = input.value;
  var pass = password.value;
  let url = "http://localhost:8000/login"
  if (username && pass) {
   HttpRequest(url, { username, pass },"post")
    .then(res => alert(res.token))
    .catch((err) => {
     console.log(err)
    })
  }
 }
}


function fileOnload () {
 var file = document.getElementById('file');
 let progress = document.getElementById('progress');
 file.onchange = function (e) {
  const reader = new FileReader();
  reader.onload = function () {
   const output = document.querySelector('#output');
   output.src = reader.result;
   var formData = new FormData();
   // 将文件转二进制
   formData.append('file', file.files[0]);

   var xhr = new XMLHttpRequest;
   xhr.open('post', 'http://localhost:8000/common/upload');
   xhr.upload.onprogress = function (ev) {// 监听上传进度
    var percent = (ev.loaded / ev.total) * 100 + '%';
    progress.innerText = percent;
   }
   xhr.send(formData);
   xhr.onreadystatechange = function () {
    if (xhr.readyState == 4 && xhr.status == 200) {
     alert("上传成功")
    }
   }
  };
  reader.readAsDataURL(e.target.files[0]);
 }
}


function uploadMultiple () {
 var fileB = document.getElementById('bigFile');
 let progress2 = document.getElementById('progress2');
 fileB.onchange = function (e) {
  const reader = new FileReader();
  reader.onload = function (fe) {
   console.log("reader.onload==>fe", fe)
   const output = document.querySelector('#output');
   output.src = reader.result;
   var formData = new FormData();
   console.log("reader.onload==>fileB.files", fileB.files)
   // 将文件转二进制
   for (const n of fileB.files) {
    formData.append('photos', n);
   }
   var content = 'hey!'; // 新文件的正文...
   var blob = new Blob([content], { type: "text/xml" });

   var xhr = new XMLHttpRequest;
   xhr.open('post', 'http://localhost:8000/common/m-upload');
   xhr.upload.onprogress = function (ev) {// 监听上传进度
    var percent = (ev.loaded / ev.total) * 100 + '%';
    progress2.innerText = percent;
   }
   xhr.send(formData);
   xhr.onreadystatechange = function () {
    if (xhr.readyState == 4 && xhr.status == 200) {
     //code
    }
   }
  };
  reader.readAsDataURL(e.target.files[0]);
 }
}